package view.layer
{
	//资源加载
	
	import command.LoginCommand;
	
	import event.GlobalEvent;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.navigateToURL;
	import flash.system.Security;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import flash3.framework.Layer;
	import flash3.net.*;
	import flash3.net.MsgPacket;
	
	import global.HtmlConnect;
	import global.Resource;
	
	import libs.bulkloader.BulkLoader;
	import libs.bulkloader.BulkProgressEvent;
	import libs.bulkloader.lazyloaders.LazyBulkLoader;
	import libs.bulkloader.lazyloaders.LazyXMLLoader;
	import libs.iframe.IFrame;
	
	import model.*;
	import model.GlobalModel;
	import model.PrizePoolModel;
	
	import net.*;
	import net.http.RequestController;
	import net.html.IFrameFactory;
	
	import util.ImageNumber;
	import util.MD5;
	
	import view.plugin.FloatText;
	import view.plugin.GameResult;
	import view.plugin.GlobalPanel;
	import view.plugin.LoaderView;
	import view.plugin.MessageBox;
	import view.plugin.PlayerInfoPanel;
	import view.plugin.SelectPanel;
	import view.plugin.UserImage;
	
	import vo.*;

	public class LayerLoader extends Layer
	{
		private var main:MovieClip = null;
		private var message:TextField = null;
		private var timer:Timer = null;
		private var connectCount:uint = 0;
		private var loginCommond:LoginCommand = null;
		private var httpRequest:RequestController = null;
		private var isGetHttpInfoEnd:Boolean = false;
		private var isLoadResourceEnd:Boolean = false;
		public function LayerLoader()
		{
			super();
			main = new ResourceLoader();
			message = main.txtMessage;
			addChild(main);
			timer = new Timer(120000);
			httpRequest = new RequestController();
		}
		
		override public function enter():void
		{
			isGetHttpInfoEnd = false;
			isLoadResourceEnd = false;
			
			message.text = "正在加载资源，请稍后...     " + "0%";
			Resource.i.loadConfig(onLoadConfigComplete);
			
			netRequestLogin();
			timer.addEventListener(TimerEvent.TIMER, onTimerComplete);
			timer.reset();
			timer.start();
			
//			var frame:IFrame = IFrameFactory.getFrameInfo();
//			this.addChild(frame);
//			MessageBox.i.show(MessageBox.BTN_OK, "连接超时，请稍后再试！", onLoginError);
		}
		
		private function netRequestLogin():void
		{
			httpRequest.request(URLRequestMethod.GET, HtmlConnect.getValidateurl(), null);
//			httpRequest.request(URLRequestMethod.GET, "http://219.146.12.66:8081/gamevalidate/gamevalidate.do?typelogin=userid&userid=10137&passpod=E10ADC3949BA59ABBE56E057F20F883E&kindid=102", null);
			httpRequest.addEventListener(Event.COMPLETE, onHttpRequestComplete);
			httpRequest.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
		}
		
		private function onIOError(evt:IOErrorEvent):void
		{
			if(++connectCount > 3)
			{
				message.text = "IO错误...";
				stopTimer();
			}
			else
			{
				netRequestLogin();
			}
		}
		
		private function onHttpRequestComplete(evt:Event):void
		{
			stopTimer();
			if(GlobalModel.loginMode == GlobalModel.LOGIN_MODE_MAOPU)
			{
				var htmlInfo:Object = HtmlConnect.getRoomList(httpRequest.data);
				
				var result:Object = htmlInfo.result;
				if(result.code == "0")
				{
					MeInfoModel.i.mdPassWord = htmlInfo.userinfo.passpod;
					MeInfoModel.i.szAccounts = htmlInfo.userinfo.nickname;
					MeInfoModel.i.userInfo.dwUserID = htmlInfo.userinfo.userid;
					MeInfoModel.i.userInfo.szAccounts = htmlInfo.userinfo.nickname;
					MeInfoModel.i.userInfo.szImageUrl = htmlInfo.userinfo.faceurl;
					MeInfoModel.i.userInfo.cbGender = htmlInfo.userinfo.gender;
					MeInfoModel.i.userScore.lGoldScore = htmlInfo.userinfo.goldscore;
					
					GlobalModel.addMoneyUrl = htmlInfo.userinfo.payurl;
					GlobalModel.loginUrl = htmlInfo.userinfo.loginurl;
					
					PrizePoolModel.i.parseJson(htmlInfo.prizelist);
					GameServerModel.i.parseJson(htmlInfo.servers);
					
					GlobalModel.i.loginInfo.szPassWord = htmlInfo.userinfo.passpod;
					GlobalModel.i.loginInfo.dwUserID = htmlInfo.userinfo.userid;
					
					isGetHttpInfoEnd = true;
					loaderFinish();
				}
				else
				{
					message.text = "请求失败...";
				}
			}
			else if(GlobalModel.loginMode == GlobalModel.LOGIN_MODE_LOCAL)
			{
				var szPassword:String = GlobalModel.i.loginInfo.szPassWord;
				MeInfoModel.i.szPassWord = szPassword;
				GlobalModel.i.loginInfo.szPassWord = MD5.hash(szPassword).toLocaleUpperCase();
			}
		}

		override public function leave():void
		{
			stopTimer();
		}

		private function onTimerComplete(evt:TimerEvent):void
		{
			stopTimer();
			MessageBox.i.show(MessageBox.BTN_OK, "连接超时，请稍后再试！", onLoginError);
		} 
		private function stopTimer():void
		{
			timer.removeEventListener(TimerEvent.TIMER, onTimerComplete);
			timer.stop();
		}
		private function onLoginError():void
		{
			if(GlobalModel.loginMode == GlobalModel.LOGIN_MODE_MAOPU)
			{
				GlobalModel.navigateToLogin();
			}
			else if(GlobalModel.loginMode == GlobalModel.LOGIN_MODE_LOCAL)
			{
				changeState(L.L_Login);
			}
		}

		private function onLoadConfigComplete():void
		{
			loadMain();
		}
		
		private function loadMain():void
		{
			stopTimer();
			Resource.i.loadModule("preloader", onLoadMainComplete, onLoadMainProgress);
		}
		private function onLoadMainIOError(evt:ErrorEvent):void
		{
			stopTimer();
			message.text = "onLoadMainIOError:IO错误！";
		}
		private function onLoadMainProgress(evt:BulkProgressEvent):void
		{
			if(evt.itemsLoaded >= evt.itemsTotal)
			{
				evt.target.removeEventListener(BulkProgressEvent.PROGRESS, onLoadMainProgress); 
			}
			message.text = "正在加载资源，请稍后...     "+(evt.weightPercent*100).toFixed(0) + "%";
		}
		private function onLoadMainComplete(evt:BulkProgressEvent):void
		{
			loadImage();
		}
		private function loadImage():void
		{
			var userImage:UserImage = MeInfoModel.i.userImage;
			if(!userImage.imageBitmap)
				userImage.loader(MeInfoModel.i.userInfo);
			onLoaderImageComplete(null);
		}
		private function loaderFinish():void
		{
			if(isLoadResourceEnd && isGetHttpInfoEnd)
			{
				message.text = "加载完成...";
				ChessLobby.loader.addChild(PlayerInfoPanel.i);
				ChessLobby.select.addChild(SelectPanel.i);
				ChessLobby.tops.addChild(FloatText.i);
				ChessLobby.common.addChild(GlobalPanel.i);
				PlayerInfoPanel.i.visible = false;
				MeInfoModel.i.userImage.removeEventListener(Event.COMPLETE, onLoaderImageComplete);
				Resource.i.loadModule("game");
				changeState(L.L_Lobby);
			}
		}
		private function onLoaderImageComplete(evt:Event):void
		{
			isLoadResourceEnd = true;
			loaderFinish();
		}
	}
}